<?php
/*
 * Created on 2017
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 
 // 超级设计师支付宝支付
 function designer_alipay($json , $session)
 {
 	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$sinfo = sql_fetch_one("select from_unixtime(unix_timestamp(),'%Y%m%d%H%i%s') as TM , rand() * 1000000 as AT") ;		// 我们自己的订单编号
	if (is_null($sinfo) || is_array($sinfo) == false)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_INVITE"] ;
		return $ret ;
	}
	
	$out_trade_no = $sinfo['TM'] . intval($sinfo['AT']) ;
	//lwx 新增 根据app传递的金额来添加
	//$money = 2000 ;
	$money=intval(trim(get_jsonValue($json,"money"))) ;
	//根据金额判断type class
	$feeprice=$GLOBALS['vip_fee'];
	$v1=intval($feeprice['v1']);
	$v2=intval($feeprice['v2']);
	$type_class=0;
	if($money==$v1){
	    $type_class=1;
	}else{
	    $type_class=5;
	}
	
	$body = "JAJAHOME 会员付年费 $money 元" ;
	
	sql_query("delete from `log_pay_vip` where `trade_no` = '$out_trade_no' and `state` not in ( 1 , 2)") ;
	sql_query("insert into `log_pay_vip` (`prepay_id` , `transaction_id`, `trade_no`, `user_type_class` ,`uid`, `type`, `starttime`, `endtime`, `memo_xml`, `rmb`, `state`, `state_count`) VALUES ('$out_trade_no' , 'zfb_$out_trade_no' , '$out_trade_no', '$type_class' , '$uid', '2', unix_timestamp(), '0', '', '$money', '0', '0') ") ;
	$save_tr_id = sql_fetch_one_cell("select `prepay_id` from `log_pay_vip` where `prepay_id` = '$out_trade_no'") ;
	if ($out_trade_no != $save_tr_id){
		$ret['status'] = $GLOBALS["ERR"]["ERR_DATABASE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_DATABASE"] ;
		return $ret ;
	}
	
	
	$zfb = new zfb_pay_class() ;
	$result = $zfb->getAppPay_unifiedorder($body , $out_trade_no , $money , $GLOBALS['ZFB_NOTIFY_URL_VIP']) ;	
	
	$ret['data']['order_string'] = 	$result ;
	return $ret ;
 }
 // 超级设计师微信支付
 function designer_wxpay($json , $session)
 {
 	$ret = array() ;
	$ret['data'] = array() ;
	
	$uid = util_set_GETUID_for_SESSION($session) ;
	if ($uid <= 0)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_USER_NOTFIND"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_USER_NOTFIND"] ;
		return $ret ;
	}
	
	$sinfo = sql_fetch_one("select from_unixtime(unix_timestamp(),'%Y%m%d%H%i%s') as TM , rand() * 1000000 as AT") ;		// 我们自己的订单编号
	if (is_null($sinfo) || is_array($sinfo) == false)
	{
		$ret['status'] = $GLOBALS["ERR"]["ERR_TRADE_INVITE"] ;
		$ret['message'] = $GLOBALS["MES"]["ERR_TRADE_INVITE"] ;
		return $ret ;
	}
	
	$out_trade_no = $sinfo['TM'] . intval($sinfo['AT']) ;
	//lwx 新增 根据app传递的金额来添加
	//$money = 2000 ;
	$money=trim(get_jsonValue($json,"money")) ;
	$body = "JAJAHOME 会员付年费 $money 元" ;
	
	$feeprice=$GLOBALS['vip_fee'];
	$v1=intval($feeprice['v1']);
	$v2=intval($feeprice['v2']);
	$type_class=0;
	if($money==$v1){
	    $type_class=1;
	}else{
	    $type_class=5;
	}
	
	$result = getWeiXin_AppPay_unifiedorder($body , $out_trade_no , $money , $GLOBALS['WX_NOTIFY_URL_VIP']) ;	
	if ($result['return_code'] == "SUCCESS" && $result['result_code'] == "SUCCESS")
	{
		$prepay_id = $result['prepay_id'] ;	// 微信返回的支付交易会话ID		
		$memo_xml = $result['document_result'] ;		
		// 统一下单成功
		sql_query("delete from `log_pay_vip` where `trade_no` = '$out_trade_no' and `state` not in ( 1 , 2)") ;
		sql_query("insert into `log_pay_vip` (`prepay_id` , `transaction_id`, `trade_no`, `user_type_class` ,`uid`, `type`, `starttime`, `endtime`, `memo_xml`, `rmb`, `state`, `state_count`) VALUES ('$prepay_id' , '$prepay_id' , '$out_trade_no', '$type_class' , '$uid', '1', unix_timestamp(), '0', '$memo_xml', '$money', '0', '0') ") ;
		$save_tr_id = sql_fetch_one_cell("select `prepay_id` from `log_pay_vip` where `prepay_id` = '$prepay_id'") ;
		if ($prepay_id != $save_tr_id){
			$ret['status'] = $GLOBALS["ERR"]["ERR_DATABASE"] ;
			$ret['message'] = $GLOBALS["MES"]["ERR_DATABASE"] ;
			return $ret ;
		}
		
		$param = array() ;
		$param["appid"]				= $GLOBALS['WX_APPID'] ;	// 公众账号ID
		$param["partnerid"]			= $GLOBALS['WX_MCHID'] ;	// 商户号
		$param["prepayid"]			= $prepay_id ;			// 微信返回的支付交易会话ID
		$param["noncestr"]			= md5("G8a2fe938d2j" . time());		// 随机字符串 不长于32位
		$param["timestamp"]			= time() ;					// 时间戳
		$param["package"]			= "Sign=WXPay" ;			// 暂填写固定值Sign=WXPay			
		$param["sign"]				= strtoupper(md5(getParamAuto($param,false) . "&key={$GLOBALS['WX_KEY']}")) ;
		
		$ret['data']['pay_info'] = array() ;
		
		
		$ret['data']['pay_info']['partnerid'] 	= $param["partnerid"] ;		// 商户号
		$ret['data']['pay_info']['prepayid'] 	= $param["prepayid"] ;		// 微信返回的支付交易会话ID
		$ret['data']['pay_info']['noncestr'] 	= $param["noncestr"] ;		// 随机字符串 不长于32位
		$ret['data']['pay_info']['timestamp'] 	= $param["timestamp"] ;		// 时间戳
		$ret['data']['pay_info']['package'] 	= $param["package"] ;		// 暂填写固定值Sign=WXPay
		$ret['data']['pay_info']['sign'] 		= $param["sign"] ;			// 签名
		$ret['data']['pay_info']['appid'] 		= $param["appid"] ;
		
		$ret['data']['pay_info']['trade_no'] 	= $out_trade_no ;			// 商户订单号
	}
	else
	{
		$ret['data']['pay_info'] = array() ;
		$ret['data']['pay_info']['error_code'] = $result['err_code'] ;
		$ret['data']['pay_info']['error_desc'] = $result['err_code_des'] ;
		
		$ret['status'] = 88 ;	// 下单失败
		$ret['message'] = "调用统一下单失败:" ;
		if ($result['return_code'] == "FAIL"){
			$ret['message'] .= $result['return_msg'] ;
		} 
		if ($result['return_code'] == "FAIL"){
			$ret['message'] .= "(" . $result['err_code'] . ")" . $result['err_code_des'];
		}
	}
	return $ret ;
 }
 
 
 
?>
